﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smart.Model;

using Smart.Data.Reporting;
using Smart.Business.CaloriesCalculator;
using Smart.Data;

namespace Smart.Business.Reporting
{
    /// <summary>
    /// Implement business logic for reporting
    /// </summary>
    public class ReportingBL
    {
        // here call Data Layer
        private ReportingDL dal = null;
        private ISmartEntities context = null;

        public ReportingBL()
        {
            if (dal == null)
            {
                dal = new ReportingDL(null);
            }
        }

        public ReportingBL(ISmartEntities context)
        {
            if (dal == null)
            {
                dal = new ReportingDL(context);
            }
        }

        public List<MealsReportView> GetMealsReport(int currentUserID)
        {
            return dal.GetMealsReport(currentUserID);
        }

        public List<ExerciseReportView> GetSportsReport(int currentUserID)
        {
            return dal.GetSportsReport(currentUserID);
        }

        public string getMealReportChartUrl(int currentUserID)
        {
            String url = "";
            List<MealsReportView> items = dal.GetMealsReport(currentUserID);

            double allCalories = 0;
            foreach (var item in items)
            {
                allCalories += (double)item.Energy;   
            }

            Dictionary<string, string> mealToPercent = new Dictionary<string, string>();
            CaloriesCalculatorBL cc = new CaloriesCalculatorBL(context);

            foreach (var item in cc.GetAllMeals())
            {
                if (!mealToPercent.ContainsKey(item.Name) && dal.getAllCaloriesForMeal(currentUserID, item.Name) != 0)
                {
                    double percent = dal.getAllCaloriesForMeal(currentUserID, item.Name) / allCalories * 100;
                    string percentStr = String.Format("{0:0.00}", (percent));
                    percentStr = percentStr.Replace(",", ".");
                    mealToPercent.Add(item.Name, percentStr);
                }   
            }

            string data = "";
            string labels = "";

            List<string> keys = mealToPercent.Keys.ToList<string>();
            foreach(string key in keys)
	        {
		         data += (mealToPercent[key] + ",");
			     labels += (key+"|"); 
	        }
          
		    if(data.Length != 0 && labels.Length != 0) {
                data = data.Remove(data.Length - 1);
                labels = labels.Remove(labels.Length - 1);
		    }

            string googleChartURL = "http://chart.apis.google.com/chart";
            string chartSize = "?chs=300x225";
            string chartType = "&cht=p3";
            string colors = "&chco=224499";
            string chartData = "&chd=t:" + data;
            string chartDataLabels = "&chdl=" + labels;
            string chartDataLabelsPosition = "&chdlp=b";
            string chartTitle = "&chtt=All Callories Report";

            url = googleChartURL + chartSize + chartType + colors + chartData + chartDataLabels + chartDataLabelsPosition + chartTitle;

            return url;  
        }

        public string getSportsReportChartUrl(int currentUserID)
        {
            String url = "";
            List<ExerciseReportView> items = dal.GetSportsReport(currentUserID);

            double allCalories = 0;
            foreach (var item in items)
            {
                allCalories += (double)item.BurnedCals;
            }

            Dictionary<string, string> mealToPercent = new Dictionary<string, string>();

            foreach (var item in dal.getAllExercises())
            {
                if (!mealToPercent.ContainsKey(item.ExerciseName) && dal.getAllCaloriesForExercise(currentUserID, item.ExerciseName) != 0)
                {
                    double percent = dal.getAllCaloriesForExercise(currentUserID, item.ExerciseName) / allCalories * 100;
                    string percentStr = String.Format("{0:0.00}", (percent));
                    percentStr = percentStr.Replace(",", ".");
                    mealToPercent.Add(item.ExerciseName, percentStr);
                }
            }

            string data = "";
            string labels = "";

            List<string> keys = mealToPercent.Keys.ToList<string>();
            foreach (string key in keys)
            {
                data += (mealToPercent[key] + ",");
                labels += (key + "|");
            }

            if (data.Length != 0 && labels.Length != 0)
            {
                data = data.Remove(data.Length - 1);
                labels = labels.Remove(labels.Length - 1);
            }

            string googleChartURL = "http://chart.apis.google.com/chart";
            string chartSize = "?chs=300x225";
            string chartType = "&cht=p3";
            string colors = "&chco=224499";
            string chartData = "&chd=t:" + data;
            string chartDataLabels = "&chdl=" + labels;
            string chartDataLabelsPosition = "&chdlp=b";
            string chartTitle = "&chtt=All Burned Callories Report";

            url = googleChartURL + chartSize + chartType + colors + chartData + chartDataLabels + chartDataLabelsPosition + chartTitle;

            return url;
        }
    }
}
